<?php
namespace Pay\Controller;
use Admin\Controller\InitController;
use Pay\Library\Method;
if (!defined('PAY_CONF')) define('PAY_CONF', APP_PATH.'Pay/conf/');
class PayController extends InitController{
	public function _initialize(){
		parent::_initialize();
		$this->db  =  D('Order');
		$this->payment_db = D('Pay/PayPayment');
		$this->member_db = D('Member');
		$this->modules_path = MODULE_PATH;
		$this->method = new Method($this->modules_path);
	}

	/**
	 * 在线充值列表
	 */
	public function pay_list(){
		$sqlMap = array();
		if(submitcheck('dosubmit')){
			$info = I('info');
			$trade_sn = $info['trade_sn'];
			$username = $info['username'];
			$start_addtime = $info['start_addtime'];
			$end_addtime = $info['end_addtime'];
			if(!empty($trade_sn)){$sqlMap['ordid'] = array("like","%$trade_sn%");}
			if(!empty($username)) $sqlMap['username'] = array("like","%$username%");;
			if(!empty($start_addtime) && !empty($end_addtime)) {
				$start = strtotime($start_addtime.' 00:00:00');
				$end = strtotime($end_addtime.' 23:59:59');
				$sqlMap[]= "AND `ordtime` >= '$start' AND  `ordtime` <= '$end'";
			}
		}
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$pay_count = $this->db->where($sqlMap)->count();
		$pay_list = $this->db->where($sqlMap)->page($pagecurr,$pagesize)->select();
		$pages = page($pay_count,$pagesize);
		$form = new \Common\Library\form();
		include $this->admin_tpl('pay_list');
	}
	/**
	 * 查询订单信息
	 */
	public function public_pay_detail(){
		$orderid = I('id');
		$order_list = $this->db->where(array('orderid'=>$orderid))->find();
		$show_header = true;
		include $this->admin_tpl('pay_detail');
	}
	/**
	 * 支付打折
	 */
	public function discount(){
		if(submitcheck('dosubmit')){
			$order = array();
			$orderid = I('id');
			$order['discount'] =  floatval($_POST['discount']);
			$infos = $this->db->where("orderid = '$orderid'")->save($order);
			if(!$infos){
				$this->error($this->db->getError());
			}
			$this->success('打折成功', 'javascript:close_dialog();');
		}else{
			$show_header = true;
			$show_validator = true;
			$orderid = I('id');
			$pay_discount = $this->db->where(array('orderid'=>$orderid))->find();
			include $this->admin_tpl('pay_discount');
		}
	}

	/**
	 * 支付取消
	 */
	public function pay_cancel(){
		$id = intval($_GET['id']);
		$order['ordstatus'] = 2;
		$result = $this->db->where("orderid = '$id'")->save($order);
		if(!$result){
			$this->error($this->db->getError());
		}
		$this->success('交易取消成功', 'javascript:close_dialog();');
	}

	/**
	 * 删除支付记录
	 */
	public function pay_del(){
		$id = intval($_GET['id']);
		$result = $this->db->where("orderid = '$id'")->delete();
		if(!$result){
			$this->error($this->db->getError());
		}
		$this->success('删除成功',U('pay_list'));
	}
	/**
	 * 支付模块列表
	 */
	public function init(){
		$infos = $this->method->get_list();
		$show_dialog = true;
		include $this->admin_tpl('payment_list');
	}
	/**
	 * 安装支付模块
	 */
	public function add(){
		if(submitcheck('dosubmit')){
			$info = $infos = array();
			$pay_code =trim($_POST['pay_code']);//支付方法
			//根据支付方法查出该方法所有字段
			$infos = $this->payment_db->where("pay_code = '$pay_code'")->find();
			$config = $infos['config'];
			$config = string2array($config);
			$cache_info = array();

			$config_value = (array)$_POST['config_value'];
			foreach ($config_value as $key => $value) {
				$config[$key]= $value;
				$cache_info[$key] = $value;
			}
			$info['config'] = array2string($config);
			$info['name'] = $_POST['name'];
			$info['pay_name'] = $_POST['pay_name'];
			$info['pay_desc'] = $_POST['description'];
			$info['pay_id'] = $_POST['pay_id'];
			$info['pay_code'] = $_POST['pay_code'];
			$info['is_cod'] = $_POST['is_cod'];
			$info['is_online'] = $_POST['is_online'];
			$info['pay_fee'] = intval($_POST['pay_fee']);
			$info['pay_method'] = intval($_POST['pay_method']);
			$info['pay_order'] = intval($_POST['pay_order']);
			$info['enabled'] = '1';
			$info['author'] = $infos['author'];
			$info['website'] = $infos['website'];
			$info['version'] = $infos['version'];

			setcache(strtolower($pay_code), $cache_info, 'pay');
			$result = $this->payment_db->add($info);
			if(!$result){
				$this->error($this->payment_db->getError());
			}
			$this->payment_db->build_cache();
			$this->success('支付接口安装成功', 'javascript:close_dialog();');
		}else{
			$pay_code = I('code');
			$infos = $this->method->get_payment($pay_code);
			extract($infos);
			$show_header = true;
			$show_validator = true;
			$form = new  \Common\Library\form();
			include $this->admin_tpl('payment_detail');
		}
	}
	/**
	 * 卸载支付模块
	 */
	public function delete(){
		$id = I('id');
		$result = $this->payment_db->where("pay_id = '$id'")->delete();
		if(!$result){
			$this->error($this->payment_db->getError());
		}
		$this->payment_db->build_cache();
		$this->success(L('delete_succ'),U('pay_list'));
	}
	/**
	 * 配置支付模块
	 */
	public function edit(){
		if(submitcheck('dosubmit')){
			$infos = $this->method->get_payment($_POST['pay_code']);
			$config = $infos['config'];
			$cache_info = array();
			foreach ($_POST['config_name'] as $key => $value) {
				$cache_info[$value] = $config[$value]['value'] = trim($_POST['config_value'][$key]);
			}	
			$info['config'] = array2string($config);
			$info['name'] = trim($_POST['name']);
			$info['pay_name'] = trim($_POST['pay_name']);
			$info['pay_desc'] = trim($_POST['description']);
			$info['pay_id'] = $_POST['pay_id'];
			$info['pay_code'] = trim($_POST['pay_code']);
			$info['pay_order'] = intval($_POST['pay_order']);
			$info['pay_method'] = intval($_POST['pay_method']);	
			$info['pay_fee']  = (intval($_POST['pay_method'])==0) ? intval($_POST['pay_rate']) : intval($_POST['pay_fix']);		
			$info['is_cod'] = trim($_POST['is_cod']);
			$info['is_online'] = trim($_POST['is_online']);
			$info['enabled'] = '1';
			$info['author'] = $infos['author'];
			$info['website'] = $infos['website'];
			$info['version'] = $infos['version'];
			$this->payment_db->save($info);
			setcache(strtolower($_POST['pay_code']), $cache_info, 'pay');
			$this->payment_db->build_cache();
			$this->success('支付接口配置变更成功', 'javascript:close_dialog();');
		}else{
			$pay_id = I('id');
			$infos = $this->payment_db->where("pay_id = '$pay_id'")->find();
			extract($infos);
			$config = string2array($config);
			$show_header = true;
			$show_validator = true;
			$form = new  \Common\Library\form();
			include $this->admin_tpl('payment_detail');
		}
	}
	/**
	 * 充值入账
	 */
	public function modify_deposit(){
		if(submitcheck('dosubmit')){
			$username = isset($_POST['username']) && trim($_POST['username']) ? trim($_POST['username']) : $this->error(L('username').L('error'));
			//交易备注
			$usernote = isset($_POST['usernote']) && trim($_POST['usernote']) ? addslashes(trim($_POST['usernote'])) :$this->error(L('usernote').L('error'));
			//根据用户名查出用户id 
			$userinfo = $this->member_db->where("username = '$username'")->field('userid')->find();
			if($userinfo){
				$sqlMap = array();
				$pay_unit = I('pay_unit');//充值额度  有增加 ‘1’  减少 ‘0’ 增加及添加一个订单，减少及在用户的账户上相应的减少
 				if($pay_unit == 1){
 					$sqlMap['ordid'] = date('YmdHis').time();
 					$userid = $sqlMap['userid'] = $userinfo['userid'];
 					$sqlMap['username'] = $username;
 					$sqlMap['ordtime'] = NOW_TIME;
 					$unit = $sqlMap['point'] = $_POST['unit']; 
 					$sqlMap['ordstatus'] = '1';
 					$sqlMap['usernote'] = $usernote;
 					$sqlMap['payment'] = '后台充值';
 					$result = $this->db->add($sqlMap);
 					point_sum($username,$userid,$unit);
 					if(!$result){
 						$this->error($this->db->getError());
 					}
 					$this->success('交易成功');
				}else{
					$userid = $userinfo['userid'];
					$unit =  $_POST['unit'];
					$sqlMap['ordid'] = date('YmdHis').time();
					$sqlMap['userid'] = $userid;
					$sqlMap['username'] = $username;
					$sqlMap['ordtime'] = NOW_TIME;
					$sqlMap['point'] = $unit;
					$sqlMap['ordstatus'] = '1';
					$sqlMap['usernote'] = $usernote;
					$sqlMap['payment'] = '后台扣除';
					$result = $this->db->add($sqlMap);
					$rs = point_num($username,$userid,$unit);
					if(!$result && !$rs){
						$this->error($this->member_db->getError());
					}
					$this->success('交易成功');
				}
			}
		}else{
			$show_validator = true;
			include $this->admin_tpl('modify_deposit');
		}
	}
	/**
	 * 检测用户名是否存在
	 */
	public function public_checkname_ajax(){
		$username = isset($_GET['username']) && trim($_GET['username']) ? trim($_GET['username']) : exit(0);
		if(CHARSET != 'utf-8') {
			$username = iconv('utf-8', CHARSET, $username);
			$username = addslashes($username);
		}
		$result = $this->member_db->where("username = '$username'")->find();
		if ($result ){
			//显示用户余额多少 点数
			exit(L('point').'  '.$result['point']);
		} else {
			exit('FALSE');
		}
	}
}